package com.github.edu.manager.fwdl.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.page.entity.Pager;
import com.github.admin.edu.oauth.server.api.entity.TSysRole;
import com.github.edu.client.common.service.impl.BaseAbstractService;
import com.github.edu.manager.fwdl.domain.FwdlManagerDomain;
import com.github.edu.manager.fwdl.service.FwdlService;
import com.github.edu.repair.api.entity.TBxFwdl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-21
 */
@Service
@Slf4j
public class FwdlServiceImpl  extends BaseAbstractService implements FwdlService {

    @Autowired
    private FwdlManagerDomain domain;

    @Override
    public JsonArray<TBxFwdl> getAllByPage(TBxFwdl tSysRole, Pager pager) {
        String code = jsonEntity(tSysRole, pager);
        JsonArray<TBxFwdl> jsonArray = null;
        if (!StringUtils.isEmpty(code)) {
            String data = domain.getALLByPager(code);
            if (!StringUtils.isEmpty(data)) {
                jsonArray = JsonUtils.toCollection(data, new TypeReference<JsonArray<TBxFwdl>>() {
                });
            }
        }
        return jsonArray;
    }

    @Override
    public TBxFwdl getEntity(Integer aLong) {
        JsonEntity<TBxFwdl> jsonEntity = JsonUtils.toCollection(domain.getEntity(aLong), new TypeReference<JsonEntity<TBxFwdl>>() {
        });
        return jsonEntity.getData();
    }

    @Override
    public TBxFwdl saveOrUpdate(TBxFwdl tSysRole) {
        JsonEntity<TBxFwdl> jsonEntity = new JsonEntity<>();
        jsonEntity.setData(tSysRole);
        String code = domain.saveOrUpdate(JsonUtils.toJson(jsonEntity));
        JsonEntity<TBxFwdl> json = JsonUtils.toCollection(code, new TypeReference<JsonEntity<TBxFwdl>>() {
        });
        if (null != json) {
            return json.getData();
        }
        return null;
    }

    @Override
    public String delete(String ids) {
        return domain.deleteAll(ids);
    }

    @Override
    public List<TBxFwdl> getAllFwdl() {
        String code=domain.getAll();
        if(!StringUtils.isEmpty(code)){
            JsonArray<TBxFwdl> jsonArray=JsonUtils.toCollection(code, new TypeReference<JsonArray<TBxFwdl>>() {
            });
            if(null!=jsonArray){

                return jsonArray.getDataList();
            }
        }
        return null;
    }
}
